package com.stars.module.grant.controller;

import com.stars.base.entity.CommonDTO;
import com.stars.base.entity.Result;
import com.stars.module.grant.entity.StarGrant;
import com.stars.module.grant.service.StarGrantService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author : moon
 * @version : 1.0
 * @date : 2023/03/22
 * @description : 权限控制器
 */
@RestController
@RequestMapping("starGrant")
public class StarGrantController {

    private final StarGrantService service;

    public StarGrantController(StarGrantService service) {
        this.service = service;
    }

    /**
     * 加载权限关联角色/用户信息数据列表
     * @author : moon
     * @date :  2023/3/29 10:54
     * @param: dto 查询条件
     * @param: targetType 权限绑定类型
     * @return: com.stars.base.entity.Result
     */
    @RequestMapping("/loadGrantTargetList")
    public Result loadGrantTargetList(@RequestBody CommonDTO dto, String targetType) {
        return Result.success(service.loadGrantTargetList(dto, targetType));
    }

    /**
     * 获取权限关系数据
     * @author : moon
     * @date :  2023/3/29 10:36
     * @param: fid 权限id->sourceId
     * @param: type 权限类型->sourceType
     * @return: com.stars.base.entity.Result
     */
    @RequestMapping("/getGrantData")
    public Result getGrantData(String fid, Integer type) {
        return Result.success(service.getGrantData(fid, type));
    }

    /**
     * 保存所选择权限数据，前端组装好所需选数据列表（先删后增）
     * @author : moon
     * @date :  2023/3/29 10:41
     * @param: sourceId 权限id
     * @param: targetType 授权类型
     * @param: list 组装好所选选数据列表
     * @return: com.stars.base.entity.Result
     */
    @RequestMapping("/saveGrant")
    public Result saveGrant(String sourceId, String targetType, @RequestBody List<StarGrant> list) {

        if(!StringUtils.hasLength(sourceId) || !StringUtils.hasLength(targetType)){
            return Result.error("参数缺失");
        }

        return Result.result(service.saveGrant(sourceId, targetType, list));
    }

    /**
     * 根据id删除权限关系数据
     * @author : moon
     * @date :  2023/3/29 10:44
     * @param: id 权限关系表id
     * @return: com.stars.base.entity.Result
     */
    @RequestMapping("/deleteGrantData")
    public Result deleteGrantDataById(String id) {
        return Result.result(service.deleteGrantDataById(id));
    }
}
